package com.chrray.position.service.web.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chrray.position.domain.*;
import com.chrray.position.entity.position.FdPostingsUserExeVo;
import com.chrray.position.entity.position.FdUserExe;
import com.chrray.position.enums.YesNoEnum;
import com.chrray.position.enums.app.FdJobStateEnum;
import com.chrray.position.enums.app.FdWorkJobStateEnum;
import com.chrray.position.mapper.position.*;
import com.chrray.position.pojo.dto.BasePostingsIdDto;
import com.chrray.position.pojo.dto.web.FdUserStateDto;
import com.chrray.position.service.web.WFdUserService;
import com.chrray.position.util.result.ResponseResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

import static com.chrray.position.util.result.ResponseResults.fail;
import static com.chrray.position.util.result.ResponseResults.success;

/**
 * @author LM
 * @date 2024/8/29 10:49
 */
@Service
public class WFdUserServiceImpl extends ServiceImpl<FdUserMapper, FdUser> implements WFdUserService {

    @Resource
    private FdJobPostingsMapper fdJobPostingsMapper;
    @Resource
    private FdUserFavoritesMapper fdUserFavoritesMapper;
    @Resource
    private FdUserPositionRelMapper fdUserPositionRelMapper;
    //教育经理
    @Resource
    private FdEduExperienceMapper fdEduExperienceMapper;
    //求职意向
    @Resource
    private FdJobIntentionsMapper fdJobIntentionsMapper;
    //工作经历
    @Resource
    private FdWorkExperienceMapper fdWorkExperienceMapper;
    //用户技能
    @Resource
    private FdUserSkillMapper fdUserSkillMapper;
    //用户证书
    @Resource
    private FdUserCertMapper fdUserCertMapper;
    @Resource
    private FdJobWorkerRelMapper fdJobWorkerRelMapper;
    @Resource
    private FdAuthPersonalMapper fdAuthPersonalMapper;

    @Resource
    private FdAuthEnterpriseMapper fdAuthEnterpriseMapper;


    @Override
    public IPage<FdUser> pageList(FdUserExe entity) {

        LambdaQueryWrapper<FdUser> queryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(entity.getName())) {
            queryWrapper.and(o -> o.eq(FdUser::getName, entity.getName()).or().like(FdUser::getId, entity.getId()));
        }
//        queryWrapper.ge(!ObjectUtils.isEmpty(entity.getWxNickname()), FdUser::getWxNickname, entity.getWxNickname());
        queryWrapper.le(!ObjectUtils.isEmpty(entity.getEndTime()), FdUser::getCreateTime, entity.getEndTime());
        queryWrapper.ge(!ObjectUtils.isEmpty(entity.getStartTime()), FdUser::getCreateTime, entity.getStartTime());

        IPage<FdUser> pageList = this.page(entity.getPage(), queryWrapper);
        pageList.getRecords().stream().forEach(
                fdUser -> {
                    LambdaQueryWrapper<FdJobPostings> lambdaQueryWrapper = new LambdaQueryWrapper();
                    lambdaQueryWrapper.eq(FdJobPostings::getUserId, fdUser.getId());
                    lambdaQueryWrapper.eq(FdJobPostings::getAdminPublished, YesNoEnum.NO.getCode());
                    Integer mySendNum = fdJobPostingsMapper.selectCount(lambdaQueryWrapper);
                    fdUser.setMySendNum(mySendNum);
                    LambdaQueryWrapper<FdJobWorkerRel> lambdaQueryWrapper1 = new LambdaQueryWrapper();
                    lambdaQueryWrapper1.eq(FdJobWorkerRel::getUserId, fdUser.getId());
                    Integer myGetNum = fdJobWorkerRelMapper.selectCount(lambdaQueryWrapper1);
                    fdUser.setMyGetNum(myGetNum);
                    LambdaQueryWrapper<FdUserFavorites> lambdaQueryWrapper2 = new LambdaQueryWrapper();
                    lambdaQueryWrapper2.eq(FdUserFavorites::getUserId, fdUser.getId());
                    lambdaQueryWrapper2.eq(FdUserFavorites::getDelFlag, YesNoEnum.NO.getCode());
                    Integer myLikeNum = fdUserFavoritesMapper.selectCount(lambdaQueryWrapper2);
                    fdUser.setMyLikeNum(myLikeNum);

                }
        );

        return pageList;
    }

    @Override
    public IPage<FdUserExe> pagePostingsUserList(FdUserExe entity) {

        IPage<FdUserExe> pageList = baseMapper.pagePostingsUserList(entity.getPage(), entity.getName(), ObjectUtils.isEmpty(entity.getGender()) ? null : entity.getGender(), entity.getExpectedSalaryStart(), entity.getExpectedSalaryEnd(), entity.getPostingsId(), entity.getJobState());
        return pageList;
    }

    @Override
    public FdPostingsUserExeVo detailById(BasePostingsIdDto dto) {
        FdUser fdUser = baseMapper.selectById(dto.getUserId());
        FdPostingsUserExeVo vo = new FdPostingsUserExeVo();
        BeanUtils.copyProperties(fdUser, vo);
        FdJobIntentions fdJobIntentions = fdJobIntentionsMapper.getByUserIdAndPostingsId(dto.getUserId());
        LambdaQueryWrapper<FdUserPositionRel> lambdaQueryWrapperRel = new LambdaQueryWrapper<>();
        lambdaQueryWrapperRel.eq(FdUserPositionRel::getUserId, dto.getUserId());
        lambdaQueryWrapperRel.eq(FdUserPositionRel::getPositionId, dto.getPostingsId());
        List<FdUserPositionRel> fdUserPositionRels = fdUserPositionRelMapper.selectList(lambdaQueryWrapperRel);
        if (!ObjectUtils.isEmpty(fdJobIntentions)) {
            fdJobIntentions.setFdUserPositionRelList(fdUserPositionRels);
        }
        vo.setFdJobIntentions(fdJobIntentions);
        LambdaQueryWrapper<FdEduExperience> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FdEduExperience::getUserId, dto.getUserId());
        List<FdEduExperience> experienceList = fdEduExperienceMapper.selectList(lambdaQueryWrapper);
        vo.setFdEduExperiences(experienceList);
        LambdaQueryWrapper<FdWorkExperience> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(FdWorkExperience::getUserId, dto.getUserId());
        List<FdWorkExperience> workExperienceList = fdWorkExperienceMapper.selectList(lambdaQueryWrapper1);
        vo.setWorkExperiences(workExperienceList);
        LambdaQueryWrapper<FdUserSkill> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(FdUserSkill::getUserId, dto.getUserId());
        List<FdUserSkill> fdUserSkills = fdUserSkillMapper.selectList(lambdaQueryWrapper2);
        vo.setFdUserSkills(fdUserSkills);
        LambdaQueryWrapper<FdUserCert> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.eq(FdUserCert::getUserId, dto.getUserId());
        List<FdUserCert> fdUserCerts = fdUserCertMapper.selectList(lambdaQueryWrapper3);
        vo.setFdUserCerts(fdUserCerts);
        FdAuthPersonal fdAuthPersonal = fdAuthPersonalMapper.selectByUserId(dto.getUserId());
        vo.setFdAuthPersonal(fdAuthPersonal);
        FdAuthEnterprise fdAuthEnterprise = fdAuthEnterpriseMapper.selectByUserId(dto.getUserId());
        vo.setFdAuthEnterprise(fdAuthEnterprise);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int employ(BasePostingsIdDto dto) {
        fdJobWorkerRelMapper.updateState(dto.getPostingsId(), dto.getUserId(), FdWorkJobStateEnum.EMPLOYED.getValue());

        FdJobPostings fdJobPostings = fdJobPostingsMapper.selectById(dto.getPostingsId());

        Integer integer = fdJobWorkerRelMapper.selectCountByJobId(dto.getPostingsId());
        if(integer>=fdJobPostings.getNumberOfWorkers()){
            fdJobPostings.setStatus(FdJobStateEnum.IN_PROGRESS.getValue());
            fdJobPostingsMapper.updateById(fdJobPostings);
        }

        return 1;


    }

    @Override
    public int upOrDownUser(FdUserStateDto dto) {
        FdUser fdUser = new FdUser();
        fdUser.setId(dto.getId());
        fdUser.setState(dto.getStatus());
        return baseMapper.updateById(fdUser);

    }

    @Override
    public ResponseResult<FdPostingsUserExeVo> selectById(Long userId) {

        FdUser fdUser = baseMapper.selectById(userId);
        FdPostingsUserExeVo vo = new FdPostingsUserExeVo();
        BeanUtils.copyProperties(fdUser, vo);
        FdJobIntentions fdJobIntentions = fdJobIntentionsMapper.getByUserIdAndPostingsId(userId);
        LambdaQueryWrapper<FdUserPositionRel> lambdaQueryWrapperRel = new LambdaQueryWrapper<>();
        lambdaQueryWrapperRel.eq(FdUserPositionRel::getUserId, userId);
        List<FdUserPositionRel> fdUserPositionRels = fdUserPositionRelMapper.selectList(lambdaQueryWrapperRel);
        if (!ObjectUtils.isEmpty(fdJobIntentions)) {
            fdJobIntentions.setFdUserPositionRelList(fdUserPositionRels);
        }
        vo.setFdJobIntentions(fdJobIntentions);
        LambdaQueryWrapper<FdEduExperience> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FdEduExperience::getUserId, userId);
        List<FdEduExperience> experienceList = fdEduExperienceMapper.selectList(lambdaQueryWrapper);
        vo.setFdEduExperiences(experienceList);
        LambdaQueryWrapper<FdWorkExperience> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(FdWorkExperience::getUserId, userId);
        List<FdWorkExperience> workExperienceList = fdWorkExperienceMapper.selectList(lambdaQueryWrapper1);
        vo.setWorkExperiences(workExperienceList);
        LambdaQueryWrapper<FdUserSkill> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(FdUserSkill::getUserId, userId);
        List<FdUserSkill> fdUserSkills = fdUserSkillMapper.selectList(lambdaQueryWrapper2);
        vo.setFdUserSkills(fdUserSkills);
        LambdaQueryWrapper<FdUserCert> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.eq(FdUserCert::getUserId, userId);
        List<FdUserCert> fdUserCerts = fdUserCertMapper.selectList(lambdaQueryWrapper3);
        vo.setFdUserCerts(fdUserCerts);
        FdAuthPersonal fdAuthPersonal = fdAuthPersonalMapper.selectByUserId(userId);
        vo.setFdAuthPersonal(fdAuthPersonal);
        FdAuthEnterprise fdAuthEnterprise = fdAuthEnterpriseMapper.selectByUserId(userId);
        vo.setFdAuthEnterprise(fdAuthEnterprise);

        return success(vo);
    }

    @Override
    public ResponseResult cancelEmploy(BasePostingsIdDto dto) {
        FdJobPostings fdJobPostings = fdJobPostingsMapper.selectById(dto.getPostingsId());
        if(!fdJobPostings.getStatus().equals( FdJobStateEnum.MATCHING.getValue())){
            return fail("职位状态不为匹配中");
        }
        fdJobWorkerRelMapper.updateState(dto.getPostingsId(), dto.getUserId(),FdWorkJobStateEnum.CANCEL.getValue());
        return  success();
    }

    @Override
    public ResponseResult<Object> complete(BasePostingsIdDto dto) {
        FdJobPostings fdJobPostings = fdJobPostingsMapper.selectById(dto.getPostingsId());
        if(!fdJobPostings.getStatus().equals( FdJobStateEnum.IN_PROGRESS.getValue())){
            return fail("职位状态不为匹配中");
        }
        fdJobWorkerRelMapper.updateState(dto.getPostingsId(), dto.getUserId(),FdWorkJobStateEnum.FINISHED.getValue());

        Integer integer = fdJobWorkerRelMapper.selectEmpCountByJobId(dto.getPostingsId());
        if(integer.equals(0)){
            fdJobPostings.setStatus(FdJobStateEnum.COMPLETED.getValue());
            fdJobPostingsMapper.updateById(fdJobPostings);
        }

        return  success();
    }
}
